var win;

function removeProject(processInstanceId){
	Ext.Msg.confirm('确认', "确定要删除吗?", function (btn) {
		if (btn == "yes") {
			Ext.Ajax.request({
				url:'../room/roomProject!removeProjectById.action?processInstanceId='+processInstanceId,
				success: function(rst,msg){
			 		var text=Ext.decode(rst.responseText);
			 		if(text.success){
			 			if(text.message==1){
			 				Ext.getCmp('h-fgys-db-grid').getStore().reload();
			 			}
			 		}
		 		}
			}
		 );
		}
	});
}

function showWindow(id) {
	win = new Ext.Window({
		id: "task_process",
		modal: true,
		title: '任务处理 ',
		layout: 'fit',
		width: 1000,
		height: 450,
		resizable: false,
		html: "<iframe frameborder='0' width='100%' height='100%' src='../workflow/task!process.action?id=" + id + "'</iframe>",
		listeners: {
			close: refresh
		}
	});
	win.show();
}

function toView(id, name) {
	win = new Ext.Window({
		id: "task_process_view",
		modal: true,
		title: '查看详情--' + name,
		layout: 'fit',
		width: 1000,
		height: 450,
		resizable: false,
		html: "<iframe frameborder='0' width='100%' height='100%' src='../workflow/task!view.action?id=" + id + "'</iframe>",
		listeners: {
			close: refresh
		}
	});
	win.show();
}

function refresh() {
	if(win){
		win.hide();
	}
	Hxcy.main.fgys.Store.fgysDbStore.load();
	Hxcy.main.fgys.Store.fgysYbStore.load();
}

/**查找*/
function search(content) {
	if (content) {
		content = content.replace(/^\s+|\s+$/g, "");
	}
	gridStore.baseParams = {processInstanceName: content};
	gridStore.load({params: {start: 0, limit: 10}});
}
/*
 * 覆盖延伸数据存储器
 */
Hxcy.main.fgys.Store={
	fgysDbStore:new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: encodeURI("../room/roomProject!findRunningTask.action?processName=室内分布建设&activityName=" + activityName),
			method: "POST"
		}),
		reader: new Ext.data.JsonReader({
			fields: ["id",'processInstanceId', "bizNumber", "name", "netType", "resolveContent", "emergencyLevel", "createTime", "activationTimeLong", "activationTime", "status"], 
			root: "rows", 
			id: "id",
			totalProperty: "total"
		})
	}),
	fgysYbStore:new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: encodeURI("../room/roomProject!findClosedTask.action?processName=室内分布建设&activityName=" + activityName),
			method: "POST"
		}),
		reader: new Ext.data.JsonReader({
			fields: ["id", "bizNumber", "name", "netType", "resolveContent", "emergencyLevel", "createTime", "activationTime", "closeTime", "action","status"], 
			root: "rows", 
			id: "id",
			totalProperty: "total"
		})
	})
};
fgys.putNS(['Store'])
var recordStart = 0;
/*
 * 覆盖延伸Grid展示数据
 */
Hxcy.main.fgys.Grid={
	/*
	 * 待办任务数据展示
	 */
	fgysDbGrid:{
		id:'h-fgys-db-grid',
		xtype:'grid',
		title:'待办事项',
		sm:new Ext.grid.RowSelectionModel({singeSelect:true}),
		columns: [
			new　Ext.grid.RowNumberer({
				header: "序号",
				width: 40,
				renderer: function(value, metaData, record, rowIndex) {
					return　recordStart + 1　+　rowIndex;
				}
			}), {
				header: "流程实例",
				dataIndex: "processInstanceId",
				sortable: true,
				align: "center",
				hidden:true
			},{
				header: "项目编号",
				dataIndex: "bizNumber",
				sortable: true,
				align: "center"
			}, {
				header: "项目名称",
				dataIndex: "name",
				sortable: true,
				align: "center"
			}, {
				header: "网络制式",
				dataIndex: "netType",
				sortable: true,
				align: "center"
			}, {
				header: "解决内容",
				dataIndex: "resolveContent",
				sortable: true,
				align: "center"
			}, {
				header: "紧急程度",
				dataIndex: "emergencyLevel",
				sortable: true,
				align: "center",
				renderer:function(value,cell,record,rowIndex,columnIndex,store){
					if(value=='一般'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_noaml.gif' />&nbsp;"+value;
					}else if(value=='可缓'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_yellow.gif' />&nbsp;"+value;
					}else if(value=='紧急'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_red.gif' />&nbsp;"+value;
					}
				}
			}, {
				header: "创建时间",
				dataIndex: "createTime",
				sortable: true,
				align: "center"
			}, {
				header: "开启时间",
				dataIndex: "activationTime",
				sortable: true,
				hidden:true,
				align: "center"
			}, {
				header: "剩余时间",
				dataIndex: "activationTimeLong",
				sortable: true,
				align: "center",
				renderer: function(value, metaData, record, rowIndex) {
					var remain = 3 * 24 * 60 * 60 * 1000 - (new Date().getTime() - value);
					if (remain < 0) {
						return "<font color='red'>已超时</font>"
					}
					var str = "";
					var day = parseInt(remain / (24 * 60 * 60 * 1000));
					if (day > 0) {
						str += (day + "天");
					}
					remain = remain % (24 * 60 * 60 * 1000);
					var h = parseInt(remain / (60 * 60 * 1000))
					if (h > 0) {
						str += (h + "小时");
					} else {
						if (day > 0) {
							str += "0小时";
						}
					}
					remain = remain % (60 * 60 * 1000);
					var m = parseInt(remain / (60 * 1000))
					str += (m + "分钟");
					return　str;
				}
			},{
				header: "操作",
				dataIndex: "operation",
				align: "center",
				renderer: function(value, metaData, record, rowIndex){
					if(activityName == '投诉需求确认' || activityName == '前期摸排测试' || activityName == '网络部审核'){
						var str="<a href=# onclick='removeProject("+record.get('processInstanceId')+")'>删除</a>";
						return str;
					}else{
						this.hidden = true;
					}
				}
			}
		],
		store:Hxcy.main.fgys.Store.fgysDbStore,
		autoScroll:true, 
		loadMask:true,
		stripeRows:true,
		viewConfig:{
			forceFit:true
		},
		bbar: new Ext.PagingToolbar({
			store: Hxcy.main.fgys.Store.fgysDbStore,
			pageSize: 10,
			displayInfo: true,
			beforePageText: "第",
			afterPageText: "/ {0}页",
			firstText: "首页",
			prevText: "上一页",
			nextText: "下一页",
			lastText: "末页",
			refreshText: "刷新",
			emptyMsg: "没有相关记录！",
			displayMsg: "当前显示第 {0} ~ {1} 条记录，总计 {2} 条记录",
			doLoad: function(start) {
			 	recordStart = start;
			 	this.store.load({params: {start: start, limit: this.pageSize}});
			}
		}),
		listeners:{
			render:function(gd){
				gd.getStore().load({params: {start: 0, limit: 10}});
			},
			rowdblclick:function(){
				var grid = Ext.getCmp('h-fgys-db-grid');
				//alert(grid);
				var record=grid.getSelectionModel().getSelected();
				var id=record.get('id');
				var name=record.get('name');
				var fgysWin = new Ext.Window({
					id: "task_process",
					modal: true,
					title: '任务处理 --'+name,
					layout: 'fit',
					width: 800,
					height: 400,
					resizable: true,
					maximized:true,                    //id is taskid          /room/roomProject!toProcess.action?taskId=${id}             
					html: "<iframe frameborder='0' width='100%' height='100%' src='../workflow/task!process.action?id=" + id + "'</iframe>",
					listeners: {
						close: refresh
					}
				});
				fgysWin.show();
			}
		}
	},
	/*
	 * 已办任务数据展示
	 */
	fgysYbGrid:{
		id:'h-fgys-yb-grid',
		xtype:'grid',
		title:'已办事项',
		store:Hxcy.main.fgys.Store.fgysYbStore,
		autoScroll:true, 
		loadMask:true,
		stripeRows:true,
		viewConfig:{
			forceFit:true
		},
		sm:new Ext.grid.RowSelectionModel({singeSelect:true}),
		columns: [
			new　Ext.grid.RowNumberer({
				header: "序号",
				width: 40,
				renderer: function(value, metaData, record, rowIndex) {
					return　recordStart + 1　+　rowIndex;
				}
			}), {
				header: "项目编号",
				dataIndex: "bizNumber",
				sortable: true,//排序
				align: "center"
			}, {
				header: "项目名称",
				dataIndex: "name",
				sortable: true,
				align: "center"
			}, {
				header: "网络制式",
				dataIndex: "netType",
				sortable: true,
				align: "center"
			}, {
				header: "解决内容",
				dataIndex: "resolveContent",
				sortable: true,
				align: "center"
			}, {
				header: "紧急程度",
				dataIndex: "emergencyLevel",
				sortable: true,
				align: "center",
				renderer:function(value,cell,record,rowIndex,columnIndex,store){
					if(value=='一般'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_noaml.gif' />&nbsp;"+value;
					}else if(value=='可缓'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_yellow.gif' />&nbsp;"+value;
					}else if(value=='紧急'){
						return "<img src='/zuoyue/scripts/hxcy/images/mode_red.gif' />&nbsp;"+value;
					}
				}
			}, {
				header: "创建时间",
				dataIndex: "createTime",
				sortable: true,
				align: "center"
			}, {
				header: "开启时间",
				dataIndex: "activationTime",
				hidden:true,
				sortable: true,
				align: "center"
			}, {
				header: "完成时间",
				dataIndex: "closeTime",
				sortable: true,
				align: "center"
			}, {
				header: "动作",
				hidden:true,
				dataIndex: "action",
				align: "center"
			}
		],
		tbar:[
				"-",
			{
				xtype: "datefield",
				id: "createdate",
				disabled:true
			},
			"-",
			{
				text: "导出EXCEL表格", 
				id:"trafertoExcel",
				disabled:true,
				handler: function() {
					var time=Ext.getCmp('createdate').getValue();
					//alert(time.format('Y-m-d'));
					var url = "../room/roomProject!exportToExcelByTime.action?closedtime="+time.format('Y-m-d');
					window.open(url);
				}
			},"-"
		],
		bbar: new Ext.PagingToolbar({
			store: Hxcy.main.fgys.Store.fgysYbStore,
			pageSize: 10,
			displayInfo: true,
			beforePageText: "第",
			afterPageText: "/ {0}页",
			firstText: "首页",
			prevText: "上一页",
			nextText: "下一页",
			lastText: "末页",
			refreshText: "刷新",
			emptyMsg: "没有相关记录！",
			displayMsg: "当前显示第 {0} ~ {1} 条记录，总计 {2} 条记录",
			doLoad: function(start) {
			 	recordStart = start;
			 	this.store.load({params: {start: start, limit: this.pageSize}});
			}
		}),
		listeners:{
			render:function(gd){
				gd.getStore().load({params: {start: 0, limit: 10}});
			},
			rowdblclick:function(){
				var grid = Ext.getCmp('h-fgys-yb-grid');
				//alert(grid);
				var record=grid.getSelectionModel().getSelected();
				var id=record.get('id');
				var name=record.get('name');
				var fgysWin = new Ext.Window({
					id: "task_process_view",
					modal: true,
					title: '详细信息 --'+name,
					layout: 'fit',
					width: 800,
					height: 400,
					resizable: true,
					maximized:true,
					html: "<iframe frameborder='0' width='100%' height='100%' src='../workflow/task!view.action?id=" + id + "'</iframe>",
					listeners: {
						close: refresh
					}
				});
				fgysWin.show();
			}
		}
	}
};
fgys.putNS(['Grid'])

Hxcy.main.fgys.TabPanel={
	fgysDbTabPanel:{
		id:'h-fgys-db-tabpanel',
		xtype : 'tabpanel',
		activeTab : 0,
		border : false,
		height:200,
		region:'center',
		tabPosition:'top',
		items:fgys.get('fgysDbGrid'),
		listeners:{
			activate:function(){
				var g=Ext.getCmp('h-fgys-db-grid');
				if(g){
					g.getStore().reload();
				}
			}
		}
	},
	fgysYbTabPanel:{
		id:'h-fgys-yb-tabpanel',
		xtype : 'tabpanel',
		activeTab : 0,
		border : false,
		height: 174,
		region:'south',
		tabPosition:'top',
		items:fgys.get('fgysYbGrid'),
		listeners:{
			activate:function(){
				var g=Ext.getCmp('h-fgys-yb-grid');
				if(g){
					g.getStore().reload();
				}
			}
		}
	}
};
fgys.putNS(['TabPanel']);

Ext.onReady(function(){
	 var viewport=new Ext.Viewport({
	 	layout:'border',
	 	defaults:{
	 		border:false
	 	},
	 	items:[
				fgys.get('fgysDbTabPanel'),
				fgys.get('fgysYbTabPanel')
		]
	 });
	 if(activityName == '开工批复'){
			var timer = Ext.getCmp('createdate');
			var search = Ext.getCmp('trafertoExcel');
			if(timer){
				timer.enable();
			}
			if(search){
				search.enable();
			}
		 }
});